package com.hysf.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@Configuration
@ConfigurationProperties(prefix = "jdbc")
public class AppConfig {
	private String driver;
	private String url;
	private String name;
	private String password;
	
	@Bean
	@Profile(value = {"prod"})
	public DataSource dataSource() {
		System.out.println("DruidDataSource");
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(name);
		dataSource.setPassword(password);
		return dataSource;
	}
	
	@Bean
	@Profile(value = {"test"})
	public DataSource comboDataSource() throws PropertyVetoException {
		System.out.println("ComboPooledDataSource");
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass(driver);
		dataSource.setJdbcUrl(url);
		dataSource.setUser(name);
		dataSource.setPassword(password);
		return dataSource;
	}
	
	@Bean
	@Profile(value = {"dev"})
	public DataSource c3p0DataSource() throws PropertyVetoException {
		System.out.println("c3p0DataSource");
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass(driver);
		dataSource.setJdbcUrl(url);
		dataSource.setUser(name);
		dataSource.setPassword(password);
		return dataSource;
	}
}
